package com.yc.droid;

import android.os.Environment;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * Created by Administrator on 2016/4/27 0027.
 */
public class DataFinally {
    public DataFinally(final String s) throws IOException {
        final File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MotionDetect");
        final File file2 = new File(file, s + ".txt");
        final File file3 = new File(file, s + ".csv");
        DataFrame.init();
        FileOutputStream fileOutputStream;
        OutputStreamWriter outputStreamWriter;
        BufferedReader bufferedReader;
        try {
            fileOutputStream = new FileOutputStream(file3, true);
            outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8");
            outputStreamWriter.write("times,quat[0],quat[1],quat[2],quat[3],accel[0],accel[1],accel[2],gyro[0],gyro[1],gyro[2],ax(g),ay(g),az(g),vx(°/s),vy(°/s),vz(°/s),pitch(°),roll(°),yaw(°)\n");
            bufferedReader = new BufferedReader(new FileReader(file2));
            final char[] array = new char[18];
            while (true) {
                final String line = bufferedReader.readLine();
                if (line == null) {
                    break;
                }
                System.out.println("readline:" + line);
                if (line.length() != 54) {
                    continue;
                }
                for (int i = 0; i < 18; ++i) {
                    array[i] = (char)(16 * this.get0(line.charAt(i * 3)) + this.get0(line.charAt(1 + i * 3)));
                }
                if (DataFrame.come_one_line(array) != 2) {
                    continue;
                }
                outputStreamWriter.write(DataFrame.getDataString() + "\n");
            }
        }
        catch (IOException ex) {
            ex.printStackTrace();
            return;
        }
        bufferedReader.close();
        outputStreamWriter.flush();
        outputStreamWriter.close();
        fileOutputStream.close();
    }

    public char get0(final char c) {
        switch (c) {
            default: {
                return 255;
            }
            case '0': {
                return 0;
            }
            case '1': {
                return 1;
            }
            case '2': {
                return 2;
            }
            case '3': {
                return 3;
            }
            case '4': {
                return 4;
            }
            case '5': {
                return 5;
            }
            case '6': {
                return 6;
            }
            case '7': {
                return 7;
            }
            case '8': {
                return 8;
            }
            case '9': {
                return 9;
            }
            case 'A': {
                return 10;
            }
            case 'B': {
                return 11;
            }
            case 'C': {
                return 12;
            }
            case 'D': {
                return 13;
            }
            case 'E': {
                return 14;
            }
            case 'F': {
                return 15;
            }
        }
    }
}
